﻿using System;
using System.Configuration;
using Common.Logging;
using Quartz;
using Wicresoft.EKA.NRemedy;

namespace Wicresoft.Task.EmployeeSync
{
    public class EmployeeSyncTask : IJob
    {
        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string ARUid = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string ARPwd = ConfigurationManager.AppSettings["ARPwd"];

        private static readonly ILog logger = LogManager.GetCurrentClassLogger();

        //private ARProxy<CTMPeopleModel> proxypo;

        public void Execute(IJobExecutionContext con)
        {
            DateTime begin = DateTime.Now;
            logger.Info("Begin to sync employees...");

            GlobalSettings.SetCharacterEncoding("UTF8");
            using (var context = new ARLoginContext(ARServer, ARUid, ARPwd))
            {
                try
                {
                    IPeopleInfo peopleInfo = new FullPeopleInfo(context);
                    //IPeopleInfo peopleInfo = new IncrementalPeopleInfo(context);
                    peopleInfo.HandlerAll();
                }
                catch (Exception e)
                {
                    logger.Error("Employee Sync", e);
                }
            }

            DateTime end = DateTime.Now;
            var span = end - begin;
            logger.InfoFormat("End of sync employees... {0} elapsed", span);
        }


    }


}
